package ui.activities;

import settings.AppSettings;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;

public final class FeedbackActivity  extends BaseActivity
{
    final public static String BUNDLE_EXTRA_KEY_MODE_POST = "post";
    final public static String BUNDLE_EXTRA_KEY_MODE_READ = "read";
    final public static String BUNDLE_EXTRA_KEY_MODE = "mode";
    final public static String BUNDLE_EXTRA_KEY_EMAIL_ADDRESS = "emailAddress";
    
    private String 	mScreenMode = BUNDLE_EXTRA_KEY_MODE_POST;
    private WebView mFeedbackWebView;
    private String 	mFeedbackUrl;
    
    private void buildFeedbackUrl()
    {
        StringBuilder urlBuilder = new StringBuilder(AppSettings.AppUrl)
                                            .append(keywords.Servlets.Feedback)
                                            .append(BUNDLE_EXTRA_KEY_MODE)
                                            .append("=").append(mScreenMode)
                                            .append("&")
                                            .append(BUNDLE_EXTRA_KEY_EMAIL_ADDRESS)
                                            .append("=").append(mLoggedInUser.getEmail());      
        
        mFeedbackUrl = urlBuilder.toString();
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setActivityView(R.layout.feedback, R.string.str_Feedback, Color.WHITE);

        Bundle bundle = getIntent().getExtras();
        if (bundle != null)
        {
            mScreenMode = bundle.getString(BUNDLE_EXTRA_KEY_MODE);
        }
        // Else - default to initial settings.

        mFeedbackWebView = (WebView)findViewById(R.id.feedbackWebView);
        loadWebView();
    }
    
    @Override
    protected void onNewIntent(Intent intent) 
    {
        super.onNewIntent(intent);
        
        Bundle bundle = intent.getExtras();
        if (bundle != null)
        {
            mScreenMode = bundle.getString(BUNDLE_EXTRA_KEY_MODE);
        }   
        // Else - keep the previous mode.
        
        loadWebView();
    } 
    
    private void loadWebView()
    {
        WebSettings webSettings = mFeedbackWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setUserAgentString(AppSettings.webViewUserAgent);	
        buildFeedbackUrl();
        mFeedbackWebView.loadUrl(mFeedbackUrl);	  		
    }
    
    @Override
    protected void onResume()
    {
        super.onResume();
        Log.v(AppSettings.DebugPrefix, "onResume " + toString());
    }
    
    @Override
    protected void onPause()
    {
        super.onPause();
        Log.v(AppSettings.DebugPrefix, "onPause " + toString());
    }
}
